package com.huarui.task.service.impl;

import org.apache.log4j.Logger;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;
import com.huarui.task.dao.TaskDao;
import com.huarui.task.dao.TaskDetailDao;
import com.huarui.task.dao.TaskOwnerDao;
import com.huarui.task.model.Task;
import com.huarui.task.service.TaskDetailService;
import com.huarui.task.service.TaskEngineService;
import com.huarui.task.service.TaskService;
import com.huarui.task.service.provider.TaskDetailProvider;
import com.huarui.task.service.provider.TaskOwnerProvider;
import com.huarui.task.service.provider.TaskProvider;

public class TaskEngineServiceImpl implements TaskEngineService {

	
	private static Logger log = Logger.getLogger(TaskEngineServiceImpl.class);
	private TaskService taskService;

	private TransactionTemplate transactionTemplate;

	private TaskDao taskDao;
	private TaskDetailDao taskDetailDao;
	private TaskOwnerDao taskOwnerDao;

	
	public TaskOwnerDao getTaskOwnerDao() {
		return taskOwnerDao;
	}

	public void setTaskOwnerDao(TaskOwnerDao taskOwnerDao) {
		this.taskOwnerDao = taskOwnerDao;
	}

	private TaskDetailService taskDetailService;

	public TaskService getTaskService() {
		return taskService;
	}

	public void setTaskService(TaskService taskService) {
		this.taskService = taskService;
	}

	public TaskDetailService getTaskDetailService() {
		return taskDetailService;
	}

	public void setTaskDetailService(TaskDetailService taskDetailService) {
		this.taskDetailService = taskDetailService;
	}

	public TaskDetailDao getTaskDetailDao() {
		return taskDetailDao;
	}

	public void setTaskDetailDao(TaskDetailDao taskDetailDao) {
		this.taskDetailDao = taskDetailDao;
	}

	public TaskDao getTaskDao() {
		return taskDao;
	}

	public void setTaskDao(TaskDao taskDao) {
		this.taskDao = taskDao;
	}

	public TransactionTemplate getTransactionTemplate() {
		return transactionTemplate;
	}

	public void setTransactionTemplate(TransactionTemplate transactionTemplate) {
		this.transactionTemplate = transactionTemplate;
	}

//	@Override
//	public boolean commit(Integer id) {
//		return false;
//	}

	@Override
	public boolean create(Integer workFlowId, String tableName,
			Integer tableId,String tableCode , Integer status, Integer statusId, String statusName, String remark, String taskName) {

		return taskService.addNewTask(workFlowId, tableName, tableId, tableCode ,status,
				statusId, statusName, remark, taskName);

	}

	/**
	 * 把某条任务转发给某个用户
	 */
//	@Override
//	public boolean transmit(final Integer id, final Integer userId) {
//
//		transactionTemplate.execute(new TransactionCallback() {
//
//			@Override
//			public Object doInTransaction(TransactionStatus status) {
//
//				boolean result = hasPermission(id, userId);
//
//				if (result == false) {
//					return Boolean.FALSE;
//				}
//
//				Task task = taskDao.getTaskById(id, true);
//
//				task.setCurrentorId(userId);
//
//				taskDao.update(task);
//				taskDetailService.appendTransmitTaskDetail(id, task
//						.getStatusName());
//
//				return Boolean.TRUE;
//			}
//
//		});
//
//		return false;
//	}

	/**
	 * 判断该用户对该任务有无权 暂时认为只能转发给本公司同事
	 * 
	 * @param id
	 * @param userId
	 * @return
	 */
//	private boolean hasPermission(Integer id, Integer userId) {
//
//		// return
//		// UserHolder.getCurrentSessionMessage().getColleagues().contains(userId);
//
//		return true;
//	}

	@Override
	public boolean rollback(final Integer id) {

		try {

			transactionTemplate.execute(new TransactionCallback() {

				@Override
				public Object doInTransaction(TransactionStatus status) {

					/**
					 * 根据id查找Task
					 */
					Task task = taskDao.getTaskById(id, true);

					if (task == null) {
						throw new IllegalArgumentException("No such a task id:"+id);
					}

					/**
					 * 找出上一个状态对应（转发不改变状态需要跳过）
					 */
					taskDetailService.rollback(id, task.getPreStatusName());

					taskDao.update(task);

					return Boolean.TRUE;
				}
			});

		} catch (RuntimeException e) {
			log.error(e.getMessage(), e);
			return Boolean.FALSE;
		}

		return Boolean.TRUE;
	}

	@Override
	public boolean create(
			TaskProvider taskProvider, TaskDetailProvider taskDetailProvider,
			TaskOwnerProvider taskOwnerProvider) {
		return taskService.addNewTask(taskProvider, taskDetailProvider,taskOwnerProvider);
	}

	@Override
	public boolean update(TaskProvider taskProvider,
			TaskDetailProvider taskDetailProvider,
			TaskOwnerProvider taskOwnerProvider) {
	
		return taskService.update(taskProvider, taskDetailProvider, taskOwnerProvider);
	}

}
